package com.appsindy.common.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appsindy.common.action.AppsindyBaseAction;
import com.appsindy.common.action.IManageTab;
import com.appsindy.common.bo.GlobalUser;
import com.appsindy.reserv.bo.HtGlobalListValue;
import com.appsindy.reserv.dao.GlobalListValueDAO;
import com.appsindy.common.dao.GlobalUserDAO;

public class GlobalUserAction extends AppsindyBaseAction implements IManageTab {
	
	private GlobalUserDAO globalUserDAO;
	private GlobalListValueDAO globalListValueDAO;
	
	private GlobalUser glbUser;

	public String execute() {
		log.info("Init GlobalUser ...");
		
		List<HtGlobalListValue> userTypes = getGlobalListValueDAO().getUserTypes();
		if(userTypes == null){
			userTypes = new ArrayList<HtGlobalListValue>();
		}
		getSession().put("S_USER_TYPES", userTypes);
		log.info("Init S_USER_TYPES ... " + userTypes.size() + " records.");
		
		//setMenuLinkList(getAuthorizationServices().getMenuLinkByLevel(1));
		getSession().put(S_TAB_CODE, MNG_USER);
		return SUCCESS;
	}
	
	public String save() {
		log.info("Save GlobalUser getUserId ... " + glbUser.getUserId());
		if(glbUser.getUserId() < 1){
			try{
				getGlobalUserDAO().insertGlobalUser(glbUser);
				log.info("Insert Success");
			} catch(Exception e){
				e.printStackTrace();
			}
		} else{
			getGlobalUserDAO().updateGlobalUser(glbUser);
			log.info("Update Success");
		}
		StringBuilder jsonResult = new StringBuilder("{");
		jsonResult.append("'status':1");
		jsonResult.append("}");
		setJsonModel(jsonResult.toString());
		return JSON_RESULT;
	}
	
	public String delete() {
		log.info("Delete GlobalUser Id = " + glbUser.getUserId());
		StringBuilder jsonResult = new StringBuilder("{");
		if(glbUser.getUserId() > 0){
			getGlobalUserDAO().deleteGlobalUser(glbUser.getUserId());
			log.info("Delete Success");
			jsonResult.append("'status':1");
		} else{
			log.info("Delete Failed");
			jsonResult.append("'status':0");
		}
		jsonResult.append("}");
		setJsonModel(jsonResult.toString());
		return JSON_RESULT;
	}
	
	public List<GlobalUser> getTableData() {
		if(glbUser == null){
			glbUser = new GlobalUser();
		}
		log.info("[GlobalUserAct.getTableData] Search UserName = " + glbUser.getUserName());
		List<GlobalUser> resultList = new ArrayList<GlobalUser>();
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userName", glbUser.getUserName());
		params.put("userTypeId", glbUser.getUserTypeId());
		params.put("enableFlag", glbUser.getEnableFlag());
		
		List<GlobalUser> globalUserList = getGlobalUserDAO().searchGlobalUser(params);
		if(globalUserList != null){
			java.util.Iterator<GlobalUser> list = globalUserList.iterator();
			while(list.hasNext()){
				GlobalUser glbUsr = list.next();
				GlobalUser gusr = new GlobalUser();
				gusr.setUserId(glbUsr.getUserId());
				gusr.setOrganizationId(glbUsr.getOrganizationId());
				gusr.setUserName(glbUsr.getUserName());
				gusr.setUserTypeId(glbUsr.getUserTypeId());
				gusr.setEmployeeId(glbUsr.getEmployeeId());
				gusr.setDisplayName(glbUsr.getDisplayName());
				gusr.setEnableFlag(glbUsr.getEnableFlag());
				//gusr.setStartDate(DateUtils.trimDate(glbUsr.getStartDate()));
				//gusr.setEndDate(DateUtils.trimDate(glbUsr.getEndDate()));
				resultList.add(gusr);
			}
		}
		log.info("Init Finished ... " + resultList.size() + " rows");
        return resultList;
    }

	public GlobalUserDAO getGlobalUserDAO() {
		return globalUserDAO;
	}
	public void setGlobalUserDAO(GlobalUserDAO globalUserDAO) {
		this.globalUserDAO = globalUserDAO;
	}
	
	public GlobalUser getGlbUser() {
		return glbUser;
	}
	public void setGlbUser(GlobalUser glbUser) {
		this.glbUser = glbUser;
	}
	
	public void setGlobalListValueDAO(GlobalListValueDAO globalListValueDAO) {
		this.globalListValueDAO = globalListValueDAO;
	}
	public GlobalListValueDAO getGlobalListValueDAO() {
		return globalListValueDAO;
	}
	
}
